package com.google.android.material.appbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.OverScroller;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import o.gr;

abstract class HeaderBehavior<V extends View> extends ViewOffsetBehavior<V> {
    private VelocityTracker ʻ;
    private int ʼ = -1;
    private boolean ˊ;
    OverScroller ˋ;
    private int ˎ = -1;
    private int ˏ;
    private Runnable ॱ;

    class a implements Runnable {
        final /* synthetic */ HeaderBehavior ˊ;
        private final CoordinatorLayout ˋ;
        private final V ˎ;

        a(HeaderBehavior headerBehavior, CoordinatorLayout coordinatorLayout, V v) {
            this.ˊ = headerBehavior;
            this.ˋ = coordinatorLayout;
            this.ˎ = v;
        }

        public void run() {
            if (this.ˎ != null && this.ˊ.ˋ != null) {
                if (this.ˊ.ˋ.computeScrollOffset()) {
                    this.ˊ.a_(this.ˋ, this.ˎ, this.ˊ.ˋ.getCurrY());
                    ViewCompat.ˏ(this.ˎ, this);
                    return;
                }
                this.ˊ.ˊ(this.ˋ, this.ˎ);
            }
        }
    }

    public HeaderBehavior(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public boolean ˎ(CoordinatorLayout coordinatorLayout, V v, MotionEvent motionEvent) {
        if (this.ʼ < 0) {
            this.ʼ = ViewConfiguration.get(coordinatorLayout.getContext()).getScaledTouchSlop();
        }
        if (motionEvent.getAction() == 2 && this.ˊ) {
            return true;
        }
        int y;
        switch (motionEvent.getActionMasked()) {
            case 0:
                this.ˊ = false;
                int x = (int) motionEvent.getX();
                y = (int) motionEvent.getY();
                if (ˎ(v) && coordinatorLayout.ˋ(v, x, y)) {
                    this.ˏ = y;
                    this.ˎ = motionEvent.getPointerId(0);
                    ˋ();
                    break;
                }
            case 1:
            case 3:
                this.ˊ = false;
                this.ˎ = -1;
                if (this.ʻ != null) {
                    this.ʻ.recycle();
                    this.ʻ = null;
                    break;
                }
                break;
            case 2:
                y = this.ˎ;
                if (y != -1) {
                    y = motionEvent.findPointerIndex(y);
                    if (y != -1) {
                        y = (int) motionEvent.getY(y);
                        if (Math.abs(y - this.ˏ) > this.ʼ) {
                            this.ˊ = true;
                            this.ˏ = y;
                            break;
                        }
                    }
                }
                break;
        }
        if (this.ʻ != null) {
            this.ʻ.addMovement(motionEvent);
        }
        return this.ˊ;
    }

    public boolean ॱ(CoordinatorLayout coordinatorLayout, V v, MotionEvent motionEvent) {
        if (this.ʼ < 0) {
            this.ʼ = ViewConfiguration.get(coordinatorLayout.getContext()).getScaledTouchSlop();
        }
        switch (motionEvent.getActionMasked()) {
            case 0:
                int y = (int) motionEvent.getY();
                if (coordinatorLayout.ˋ(v, (int) motionEvent.getX(), y) && ˎ(v)) {
                    this.ˏ = y;
                    this.ˎ = motionEvent.getPointerId(0);
                    ˋ();
                    break;
                }
                return false;
                break;
            case 1:
                if (this.ʻ != null) {
                    this.ʻ.addMovement(motionEvent);
                    this.ʻ.computeCurrentVelocity(1000);
                    ˏ(coordinatorLayout, (View) v, -ˊ(v), 0, this.ʻ.getYVelocity(this.ˎ));
                    break;
                }
                break;
            case 2:
                int findPointerIndex = motionEvent.findPointerIndex(this.ˎ);
                if (findPointerIndex != -1) {
                    findPointerIndex = (int) motionEvent.getY(findPointerIndex);
                    int i = this.ˏ - findPointerIndex;
                    if (!this.ˊ && Math.abs(i) > this.ʼ) {
                        this.ˊ = true;
                        i = i > 0 ? i - this.ʼ : i + this.ʼ;
                    }
                    if (this.ˊ) {
                        this.ˏ = findPointerIndex;
                        ˏ(coordinatorLayout, (View) v, i, ˋ(v), 0);
                        break;
                    }
                }
                return false;
                break;
            case 3:
                break;
        }
        this.ˊ = false;
        this.ˎ = -1;
        if (this.ʻ != null) {
            this.ʻ.recycle();
            this.ʻ = null;
        }
        if (this.ʻ != null) {
            this.ʻ.addMovement(motionEvent);
        }
        return true;
    }

    int a_(CoordinatorLayout coordinatorLayout, V v, int i) {
        return ˋ(coordinatorLayout, v, i, Integer.MIN_VALUE, ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED);
    }

    int ˋ(CoordinatorLayout coordinatorLayout, V v, int i, int i2, int i3) {
        int ॱ = ॱ();
        if (i2 == 0 || ॱ < i2 || ॱ > i3) {
            return 0;
        }
        int ॱ2 = gr.ॱ(i, i2, i3);
        if (ॱ == ॱ2) {
            return 0;
        }
        ˊ(ॱ2);
        return ॱ - ॱ2;
    }

    int ˏ() {
        return ॱ();
    }

    final int ˏ(CoordinatorLayout coordinatorLayout, V v, int i, int i2, int i3) {
        return ˋ(coordinatorLayout, v, ˏ() - i, i2, i3);
    }

    final boolean ˏ(CoordinatorLayout coordinatorLayout, V v, int i, int i2, float f) {
        if (this.ॱ != null) {
            v.removeCallbacks(this.ॱ);
            this.ॱ = null;
        }
        if (this.ˋ == null) {
            this.ˋ = new OverScroller(v.getContext());
        }
        this.ˋ.fling(0, ॱ(), 0, Math.round(f), 0, 0, i, i2);
        if (this.ˋ.computeScrollOffset()) {
            this.ॱ = new a(this, coordinatorLayout, v);
            ViewCompat.ˏ(v, this.ॱ);
            return true;
        }
        ˊ(coordinatorLayout, v);
        return false;
    }

    void ˊ(CoordinatorLayout coordinatorLayout, V v) {
    }

    boolean ˎ(V v) {
        return false;
    }

    int ˋ(V v) {
        return -v.getHeight();
    }

    int ˊ(V v) {
        return v.getHeight();
    }

    private void ˋ() {
        if (this.ʻ == null) {
            this.ʻ = VelocityTracker.obtain();
        }
    }
}
